//
//  TTHTTPRequest.h
//  Tint
//
//  Created by Kevin Wu on 2/23/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "TTCore.h"
#import "TTAdditions.h"
#import "TTBasic.h"


@interface TTHTTPRequest : TTOperation<NSURLConnectionDelegate, NSURLConnectionDataDelegate> {
    
    // Property
    NSString *_URLAddress;
    NSString *_destinationPath;
    NSString *_runLoopMode;
    BOOL _shouldUpdateNetworkActivityIndicator;
    
    // Request
    NSMutableDictionary *_requestHeaders;
    NSData *_requestBody;
    NSURLConnection *_connection;
    NSString *_requestMethod;
    NSTimeInterval _timeoutInterval;
    NSURLRequestCachePolicy _cachePolicy;
    
    // Response
    NSHTTPURLResponse *_response;
    NSMutableData *_responseData;
    NSFileHandle *_responseFileHandle;
    id _parsedObject;
    
    // Progress
    BOOL _showAccurateProgress;
    CGFloat _progress;
    NSUInteger _lastBytesRead;
    NSUInteger _totalBytesRead;
    NSUInteger _contentLength;
}

@property(nonatomic, copy) NSString *URLAddress;
@property(nonatomic, copy) NSString *destinationPath;
@property(nonatomic, copy) NSString *runLoopMode;
@property(nonatomic, assign) BOOL shouldUpdateNetworkActivityIndicator;

@property(nonatomic, readonly) NSDictionary *requestHeaders;
@property(nonatomic, readonly) NSData *requestBody; // Set request type to POST if this is not nil
@property(nonatomic, readonly) NSURLConnection *connection;
@property(nonatomic, copy) NSString *requestMethod; // Default: GET
@property(nonatomic, assign) NSTimeInterval timeoutInterval; // Default: 45 seconds
@property(nonatomic, assign) NSURLRequestCachePolicy cachePolicy; // Default: NSURLRequestUseProtocolCachePolicy

@property(nonatomic, readonly) NSHTTPURLResponse *response;
@property(nonatomic, readonly) NSData *responseData;
@property(nonatomic, readonly) NSFileHandle *responseFileHandle;
@property(nonatomic, readonly) id parsedObject;

@property(nonatomic, assign) BOOL showAccurateProgress;
@property(nonatomic, readonly) CGFloat progress;
@property(nonatomic, readonly) NSUInteger lastBytesRead;
@property(nonatomic, readonly) NSUInteger totalBytesRead;
@property(nonatomic, readonly) NSUInteger contentLength;



- (id)initWithURLAddress:(NSString *)URLAddress;

- (void)startAsynchronous;

- (NSString *)responseString;
- (NSDictionary *)responseHeaders;
- (id)parse;

- (void)addRequestHeader:(NSString *)header value:(NSString *)value; // Pass nil or empty value to delete this header
- (void)setRequestHeaders:(NSDictionary *)headers;
- (void)setRequestBody:(NSData *)body;

+ (NSOperationQueue *)HTTPRequestOperationQueue; // Default maxConcurrentOperationCount = 4
+ (NSThread *)HTTPRequestThread;

@end
